package sgbag1.Vues;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;

import java.awt.geom.Point2D;

import sgbag1.Noyau.Rail;

public class VueRail extends VueElement {
    
    private Point positionFin;
    private Color couleur = new Color(10, 10, 10);
    private float largeur = (float)3.0;
    private Rail rail;
    
    public VueRail(Rail r){
        super(new Point(r.getDepart().getPosition()));
        rail = r;
        positionFin = new Point(r.getArrivee().getPosition());
    }
    
    public VueRail(Point pDebut, Point pFin){
        super(pDebut);
        positionFin = pFin;
    }
    
    public VueRail(){
        this(new Point(0,0), new Point(0,0));
    }
    
    @Override
    public void setTauxConversion(double t){
        super.setTauxConversion(t);
        position = new Point(rail.getDepart().getPosition());
        positionFin = new Point(rail.getArrivee().getPosition());
        /*position.x *= tauxConversion;
        position.y *= tauxConversion;*/
        position.setLocation(position.getX()*tauxConversion, position.getY()*tauxConversion);

        positionFin.x *= tauxConversion;
        positionFin.y *= tauxConversion;
        
        this.CalculerBoiteEnglobante();
    }
    
    @Override
    public void setDecalage(Point p){
        super.setDecalage(p);
        //position.translate(decalage.x, decalage.y);
        position.setLocation(position.getX()+decalage.getX(), position.getY()+decalage.getY());

        positionFin.translate((int)decalage.getX(), (int)decalage.getY());
        this.CalculerBoiteEnglobante();
    }
    
    public Rail getObjetNoyau(){
        return rail;
    }
    
    @Override
    public void Dessiner(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        
        float dash[] = { 4.0f };
        
        g2.setColor(couleur);
        Stroke tempStroke = g2.getStroke();
        g2.setStroke(new BasicStroke(largeur));
        g2.drawLine((int)position.getX(), (int)position.getY(), positionFin.x, positionFin.y);
        g2.setStroke(new BasicStroke((float)(2.5 * largeur), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 5.0f, dash, 0.0f));
        g2.drawLine((int)position.getX(), (int)position.getY(), positionFin.x, positionFin.y);
        g2.setStroke(tempStroke);
        
        super.Dessiner(g);
    }
    
    @Override
    public void CalculerBoiteEnglobante(){
        boiteEnglobante = new Polygon();
        double angle;
        if((positionFin.x - position.x) < 2){
            angle = Math.PI/2;    
        }
        else if((positionFin.y - position.y) < 2){
            angle = 0.0;
        }
        else{
            angle = Math.atan(positionFin.y - position.y / positionFin.x - position.x);
        }
        int delta = 7;
        Point p1 = new Point((int)(position.x - delta * Math.sin(angle)), (int)(position.y - delta * Math.cos(angle)));
        Point p2 = new Point((int)(position.x + delta * Math.sin(angle)), (int)(position.y + delta * Math.cos(angle)));
        Point p3 = new Point((int)(positionFin.x - delta * Math.sin(angle)), (int)(positionFin.y - delta * Math.cos(angle)));
        Point p4 = new Point((int)(positionFin.x + delta * Math.sin(angle)), (int)(positionFin.y + delta * Math.cos(angle)));
        ((Polygon)boiteEnglobante).addPoint(p1.x, p1.y);
        ((Polygon)boiteEnglobante).addPoint(p2.x, p2.y);
        ((Polygon)boiteEnglobante).addPoint(p4.x, p4.y);
        ((Polygon)boiteEnglobante).addPoint(p3.x, p3.y);        
    }
    
}
